/* eslint-disable */
import Request from './request1.js';
import $config from '@/config.js';

const http1 = new Request();

// request全局参数设置
http1.setConfig(config => {
    /* 设置全局配置 */
    config.baseUrl = $config.baseUrl; /* 根域名不同 */
    const systemInfo = uni.getSystemInfoSync();
    const systemInfoHeaders = {
        'device-name': systemInfo.brand, // 设备名称
        width: systemInfo.screenWidth, // 屏幕宽度
        height: systemInfo.screenHeight, // 屏幕高度
        os: systemInfo.platform, // 客户端平台
        'os-version': systemInfo.system // 操作系统版本
    };
    config.header = {
        ...config.header,
        ...systemInfoHeaders
    };
    return config;
});

let isRefreshing = false;
let requests = [];
http1.interceptor.request(
    config => {
        config.header['token'] = uni.getStorageSync('token');
        /* 请求之前拦截器 */
        config.header['client'] = 'app';
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

http1.interceptor.response(
    async response => {
        console.log("response", response)
        /* 请求之后拦截器 */
        switch (response.data.code) {
            case 0:
                return response.data;
            case -2:
                uni.$u.toast(response.data.msg);
                console.log("未登录")
                uni.navigateTo({
                    url: `/pages/login/login`,
                });
                return Promise.reject(response.data.msg);
            default:
                uni.$u.toast(response.data.msg);
                return Promise.reject(response.data.msg);
        }
    },
    error => {
        return Promise.reject(error);
    }
);

export {
    http1
};
